package com.sch.www.sys.services.impl;

import com.sch.www.common.base.BaseServicesSimpleImp;
import com.sch.www.sys.orm.dao.manual.RoleToResourceMapper;
import com.sch.www.sys.orm.entity.manual.RoleToResource;
import com.sch.www.sys.orm.entity.manual.RoleToResourceExample;
import com.sch.www.sys.services.RoleToResourceServices;
import com.sch.www.sys.services.ResourceServices;
import com.sch.www.sys.services.RoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleToResourceServicesImpl extends BaseServicesSimpleImp<RoleToResource, RoleToResourceExample, RoleToResourceMapper, RoleToResourceServices> implements RoleToResourceServices {
    @Autowired
    private ResourceServices resourceServices;

    @Autowired
    private RoleServices roleServices;

    @Override
    protected String getTableName() {
        return "";
    }

    @Override
    protected RoleToResourceExample getExampleNewInstance() {
        return new RoleToResourceExample();
    }

    @Override
    public List<String> selectRoleIdByResourceId(String... resourceIds) {
        if (isNull(resourceIds)) {
            return new ArrayList<>();
        } else {
            RoleToResourceExample example = getExampleNewInstance();
            if (resourceIds.length == 1) {
                example.createCriteria().andResourceIdEqualTo(resourceIds[0]);
            } else {
                example.createCriteria().andResourceIdIn(Arrays.asList(resourceIds));
            }
            List<RoleToResource> resourceLinkRoleList = getMapper().selectByExample(example);
            List<String> roleIdList = resourceLinkRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
            return roleIdList;
        }
    }

    @Override
    public List<String> selectResourceIdByRoleId(String... roleIds) {
        if (isNull(roleIds)) {
            return new ArrayList<>();
        } else {
            RoleToResourceExample example = getExampleNewInstance();
            if (roleIds.length == 1) {
                example.createCriteria().andRoleIdEqualTo(roleIds[0]);
            } else {
                example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
            }
            List<RoleToResource> resourceLinkRoleList = getMapper().selectByExample(example);
            List<String> resourceIdList = resourceLinkRoleList.stream().map(item -> item.getResourceId()).collect(Collectors.toList());
            return resourceIdList;
        }
    }

    @Override
    public int deleteByRoleId(String... roleIds) {
        if (isNull(roleIds)) {
            return 0;
        } else {
            RoleToResourceExample example = getExampleNewInstance();
            if (roleIds.length == 1) {
                example.createCriteria().andRoleIdEqualTo(roleIds[0]);
            } else {
                example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
            }
            int result = getMapper().deleteByExample(example);
            return result;
        }
    }


    @Transactional
    @Override
    public void insert(String roleId, List<String> resourceIdList) {
        if (roleServices.selectById(roleId) == null) {
            throw new IllegalArgumentException("不存roleId为" + roleId);
        }
        getCurrentProxy().deleteByRoleId(roleId);
        if (!CollectionUtils.isEmpty(resourceIdList)) {

            List<RoleToResource> insertList = new ArrayList<>();
            resourceIdList.forEach(resourceId -> {
                RoleToResource resourceLinkRole = new RoleToResource();
                resourceLinkRole.setResourceId(resourceId);
                resourceLinkRole.setRoleId(roleId);
                insertList.add(resourceLinkRole);
            });
            getCurrentProxy().insertSelective(insertList.toArray(new RoleToResource[]{}));
        }
    }
}
